package com.donggua.aichebao.commonui.info;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.donggua.aichebao.commonui.R;

/**
 * Created by fzh on 2017/8/7.
 */

 class InfoItemLayoutOperator {
    private RelativeLayout parentLayout;
    public static final int LEFT = 0;
    public static final int RIGHT = 1;
    public static final int TEXTVIEW = 0;
    public static final int  RADIOBUTTON = 1;
    public static final int IMAGEVIEW = 2;
    public static final int EDITTEXT =3 ;
    private String editHint;
    private TextView leftTextView;
    private TextView rightTextView;
    private EditText rightEditText;
    private ImageView rightImageView;
    private ImageView endImageView;
    private TextView infoTopView;
    private Drawable rightDrawable;
    private Drawable leftDrawable;
    private Drawable endDrawable;
    private int leftId = 100;
    private int rightId = 101;
    private int endImagId = 102;
    private String leftText;
    private String rightText;
    private int leftTextColor;
    private int rightTextColor;
    private int lineColor = 0xff000000;
    private float leftTextSize;
    private float rightTextSize;
    private final int defaultColor = 0xff000000;
    private final int defaultSize = 15;
    private int rightStyle = 0;
    private boolean hasLine = false;
    private int lineWidth = -1;
    private int lineHeight = -1;
    private boolean hasInfoTop = false;
    private float density;
    private int leftDrawablePadding=0;
    private int rightDrawablePadding = 0;
    private boolean isShowEndImg = true;
    private int rightViewLocal = LEFT;
    private boolean isRightLocal = true;
    private int textSizeType = TypedValue.COMPLEX_UNIT_PX;
    public InfoItemLayoutOperator(Context context, AttributeSet attrs,RelativeLayout parentLayout) {

        // TODO Auto-generated constructor stub
        this.parentLayout = parentLayout;
        density  = context.getResources().getDisplayMetrics().density;
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomInfo);
        this.leftText 		= ta.getString(R.styleable.CustomInfo_leftText);
        this.leftTextSize 	= ta.getDimension(R.styleable.CustomInfo_leftTextSize, defaultSize);
        this.leftTextColor 	= ta.getColor(R.styleable.CustomInfo_leftTextColor, defaultColor);
        leftDrawable		= ta.getDrawable(R.styleable.CustomInfo_infoLftDrawable);
        leftDrawablePadding = (int)ta.getDimension(R.styleable.CustomInfo_leftDrawablePadding,0);
        this.rightText 		= ta.getString(R.styleable.CustomInfo_rightText);
        this.rightTextSize 	= ta.getDimension(R.styleable.CustomInfo_rightTextSize, defaultSize);
        this.rightTextColor = ta.getColor(R.styleable.CustomInfo_rightTextColor,defaultColor);
        rightDrawable		= ta.getDrawable(R.styleable.CustomInfo_rightDrawable);
        endDrawable         = ta.getDrawable(R.styleable.CustomInfo_endDrawable);
        rightDrawablePadding = (int)ta.getDimension(R.styleable.CustomInfo_rightDrawablePadding,0);
        rightStyle 		   	= ta.getInt(R.styleable.CustomInfo_rightStyle,TEXTVIEW);
        hasLine				= ta.getBoolean(R.styleable.CustomInfo_hasLine, false);
        lineWidth			= (int)ta.getDimension(R.styleable.CustomInfo_lineWidth,-1);
        lineHeight			= (int)ta.getDimension(R.styleable.CustomInfo_lineHeight,-1);
        lineColor			= ta.getColor(R.styleable.CustomInfo_infoLineColor, defaultColor);
        hasInfoTop			= ta.getBoolean(R.styleable.CustomInfo_hasTopInfo, false);
        isShowEndImg      = ta.getBoolean(R.styleable.CustomInfo_showEndImg,true);
        editHint           = ta.getString(R.styleable.CustomInfo_editHint);
        isRightLocal       = ta.getBoolean(R.styleable.CustomInfo_isRightLocal,true);
        init(context, attrs);
        ta.recycle();

    }
    private void addView(View view,RelativeLayout.LayoutParams layoutParams){
        parentLayout.addView(view,layoutParams);
    }
    private void addView(View view){
        parentLayout.addView(view);
    }
    public void setLeftText(String text)
    {
        if(leftTextView!=null)
            leftTextView.setText(text);
    }
    public void setRightText(String text)
    {
        if(rightTextView != null )
            rightTextView.setText(text);
    }

    public String getRightText( )
    {
        String res = null;

        if(rightEditText != null ) {
            res = rightEditText.getText().toString();
        }

        return res;
    }

    private int dpTopx(int dp)
    {
        return (int)(dp*density+0.5);
    }
    private void init(Context context, AttributeSet attrs){

        leftTextView =  getTextView(context);
        leftTextView.setId(leftId);
        RelativeLayout.LayoutParams leftLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);

        leftLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL,RelativeLayout.TRUE);
        leftLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,RelativeLayout.TRUE);
        leftTextView.setText(leftText);
        leftTextView.setTextColor(leftTextColor);
        leftTextView.setTextSize(textSizeType,leftTextSize);
        if(leftDrawable!=null){
            leftDrawable.setBounds(0, 0, leftDrawable.getMinimumWidth(), leftDrawable.getMinimumHeight());//必须设置图片大小，否则不显示
            leftTextView.setCompoundDrawables(leftDrawable,null,null, null);
            leftTextView.setCompoundDrawablePadding(leftDrawablePadding);
        }

        addView(leftTextView,leftLayoutParams);
        if(rightStyle==TEXTVIEW){

        }

        if(isShowEndImg){
            createEmdImg(context);
        }
        switch (rightStyle) {
            case TEXTVIEW:
                createRightTextView(context);
                break;
            case EDITTEXT:
                createRightEditText(context);
                break;
            case IMAGEVIEW:
                createRightImageView(context);
                break;
            default:
                break;
        }
        if(hasLine){
            createLine(context);
        }
        if(hasInfoTop){
            createInfoTop(context);
        }


    }

    private void createEmdImg(Context context){
        endImageView = new ImageView(context);
        endImageView.setId(endImagId);
        RelativeLayout.LayoutParams rightLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        rightLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL,RelativeLayout.TRUE);
        rightLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,RelativeLayout.TRUE);
        addView(endImageView,rightLayoutParams);
        if(endDrawable!=null)
            endImageView.setImageDrawable(endDrawable);
    }

    public void setInfoTopShow(boolean is)
    {

        if(infoTopView==null)return;
        if(is){
            infoTopView.setVisibility(View.VISIBLE);
        }else{
            infoTopView.setVisibility(View.GONE);
        }
    }

    public void setRightTextShow(boolean is)
    {
        if(rightTextView==null)return;
        if(is){
            rightTextView.setVisibility(View.VISIBLE);
        }else{
            rightTextView.setVisibility(View.GONE);
        }
    }
    public void setInfoTopText(String text)
    {
        if(infoTopView==null)return;
        infoTopView.setText(text);
    }
    private void createInfoTop(Context context)
    {
        int w = dpTopx(15);
        int h = dpTopx(15);
        infoTopView = new TextView(context);
        RelativeLayout.LayoutParams infoTopParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        infoTopParams.addRule(RelativeLayout.RIGHT_OF,leftId);
        infoTopParams.addRule(RelativeLayout.ALIGN_TOP,leftId);
        infoTopParams.width = w;
        infoTopParams.height = h;
        infoTopParams.bottomMargin=h/2;
        infoTopParams.rightMargin=dpTopx(5);
        infoTopView.setGravity(Gravity.CENTER);
        infoTopView.setText("1");
        infoTopView.setBackgroundColor(0xffff0000);
        infoTopView.setLayoutParams(infoTopParams);
        addView(infoTopView);
    }
    private void createLine(Context context)
    {
        View line = new View(context);
        RelativeLayout.LayoutParams lineParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        if(lineHeight==-1){
            lineParams.height=2;
        }else{
            lineParams.height=lineHeight;
        }

        if(lineWidth!=-1){
            lineParams.width = lineWidth;
        }
        lineParams.addRule(RelativeLayout.CENTER_VERTICAL,RelativeLayout.TRUE);
        lineParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,RelativeLayout.TRUE);
        line.setLayoutParams(lineParams);
        line.setBackgroundColor(lineColor);
        addView(line);
    }

    private RelativeLayout.LayoutParams getRightLayoutParams()
    {
        RelativeLayout.LayoutParams rightLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        rightLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL,RelativeLayout.TRUE);

        if(isRightLocal){
            if(isShowEndImg){
                rightLayoutParams.addRule(RelativeLayout.LEFT_OF,endImagId);
                rightLayoutParams.leftMargin = dpTopx(10);
            }else{
                rightLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,RelativeLayout.TRUE);
            }
        }else{
            rightLayoutParams.addRule(RelativeLayout.RIGHT_OF,leftId);
            rightLayoutParams.leftMargin = dpTopx(10);
        }

        return rightLayoutParams;
    }
    private void createRightTextView(Context context)
    {
        rightTextView =  getTextView(context);
        rightTextView.setId(rightId);
        RelativeLayout.LayoutParams rightLayoutParams = getRightLayoutParams();
        addView(rightTextView,rightLayoutParams);
        rightTextView.setText(rightText);
        rightTextView.setTextSize(textSizeType,rightTextSize);
        rightTextView.setTextColor(rightTextColor);
        //rightDrawable = context.getResources().getDrawable(R.drawable.ic_launcher);
        /*
        if(rightDrawable!=null){
            rightDrawable.setBounds(0, 0, rightDrawable.getMinimumWidth(), rightDrawable.getMinimumHeight());//必须设置图片大小，否则不显示
            rightTextView.setCompoundDrawables(null,null, rightDrawable, null);
            rightTextView.setCompoundDrawablePadding(rightDrawablePadding);
        }*/

    }

    private void createRightEditText(Context context)
    {
        rightEditText = new EditText(context);
        rightEditText.setId(rightId);
        RelativeLayout.LayoutParams rightLayoutParams = getRightLayoutParams();
        rightEditText.setSingleLine(true);
        rightEditText.setGravity(Gravity.CENTER_VERTICAL|Gravity.RIGHT);
        rightLayoutParams.height= ViewGroup.LayoutParams.MATCH_PARENT;
        rightLayoutParams.bottomMargin=dpTopx(5);
        rightLayoutParams.topMargin=dpTopx(5);
        rightLayoutParams.rightMargin = dpTopx(10);
        addView(rightEditText,rightLayoutParams);
        rightEditText.setTextSize(TypedValue.COMPLEX_UNIT_SP,rightTextSize);
        rightEditText.setBackgroundResource(0);
        rightEditText.setTextColor(rightTextColor);
        rightEditText.setHint(editHint);

    }

    private void createRightImageView(Context context)
    {
        rightImageView = new ImageView(context);
        rightImageView.setId(rightId);
        RelativeLayout.LayoutParams rightLayoutParams = getRightLayoutParams();
        addView(rightImageView,rightLayoutParams);
        if(rightDrawable!=null)
            rightImageView.setImageDrawable(rightDrawable);
    }

    private TextView getTextView(Context context)
    {
        TextView textView = new TextView(context);
        textView.setGravity(Gravity.CENTER_VERTICAL);
        textView.setTextColor(0xff000000);
        textView.setTextSize(20);
        return textView;
    }

    public void setRightArray(Drawable drawable){
        endDrawable = drawable;
        endImageView.setImageDrawable(endDrawable);
    }
}
